# Maintainer: Oscar Fuentes <ofv@wanadoo.es>

_realname=civetweb
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.16
pkgrel=1
pkgdesc="Embedded C/C++ web server (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/civetweb/civetweb"
msys2_references=(
  "cpe: cpe:/a:civetweb_project:civetweb"
)
license=("spdx:MIT")
depends=("${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/civetweb/civetweb/archive/v$pkgver/${_realname}-${pkgver}.tar.gz")
sha256sums=('f0e471c1bf4e7804a6cfb41ea9d13e7d623b2bcc7bc1e2a4dd54951a24d60285')

build() {
    mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

    declare -a extra_config
    if check_option "debug" "n"; then
      extra_config+=("-DCMAKE_BUILD_TYPE=Release")
    else
      extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
    fi

    CFLAGS+=" -Wno-strict-prototypes -Wno-incompatible-pointer-types" \
    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
        ${MINGW_PREFIX}/bin/cmake \
        -G "Ninja" \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
        "${extra_config[@]}" \
        -DBUILD_SHARED_LIBS=YES \
        -DCIVETWEB_BUILD_TESTING=NO \
        -DCIVETWEB_ENABLE_WEBSOCKETS=YES \
        -DCIVETWEB_ENABLE_IPV6=YES \
        -DCIVETWEB_ENABLE_CXX=NO \
        -DCIVETWEB_ENABLE_SERVER_STATS=YES \
        -DCIVETWEB_SSL_OPENSSL_API_1_1=YES \
        -DCIVETWEB_ENABLE_SSL_DYNAMIC_LOADING=NO \
        -DLIBRT_LIBRARIES= \
        "${srcdir}/${_realname}-${pkgver}"

    ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
    cd "${srcdir}/build-${MSYSTEM}"
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

    local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
    for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/${_realname}/*.cmake; do
        sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
        sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    done

    install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.md \
        "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
